/**
 * @author Pawel Gadecki <pidzama@gmail.com>
 * 
 */
package spwodne;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.*;

public class Klient
        implements Serializable {

    final static long serialVersionUID = 2401626605673927978L;

    public Klient(Integer nrsp, Integer nrws, Integer nrkli, String im, String naz, String ulica, String miejscowosc,
            String kod, String pes, Float p) {
        imie = im;
        nazwisko = naz;
        pesel = pes;
        pow = p;
        this.ulica = ulica;
        this.miejscowosc = miejscowosc;
        this.kod = kod;
        naleznosci = new ArrayList();
        wplaty = new ArrayList();
        komornik = new ArrayList();
        naleznosc = Float.valueOf(0.0F);
        wysl_zapl = new Boolean(false);
        wysl_upom = new Boolean(false);
        wysl_tyt = new Boolean(false);
        nrSp = nrsp;
        nrWsi = nrws;
        nrKli = nrkli;
        sumujNaleznosci();
        sumujWplaty();
    }

    public static Float round(float val) {
        double vval = (double) val * 100D;
        float tmp = Math.round(vval);
        return new Float(tmp / 100F);
    }

    private void sumujNaleznosci() {
        Float s = new Float(0.0F);
        Float z = new Float(0.0F);
        for (Iterator i$ = naleznosci.iterator(); i$.hasNext();) {
            Naleznosc n = (Naleznosc) i$.next();
            s = round(s.floatValue() + n.kwota.floatValue());
            z = round(z.floatValue() + n.zostalo.floatValue());
        }

        naleznosc = s;
        sumaZostalo = z;
    }

    public Float getSumaWplat() {
        return sumaWplat;
    }

    public void wplac(Float kwota, Date d) {
        Float tmp = round(kwota.floatValue());
        if (sumaZostalo.floatValue() > 0.0F) {
            for (int i = 0; tmp.floatValue() > 0.0F; i++) {
                if (naleznosci.size() > i) {
                    if (((Naleznosc) naleznosci.get(i)).zostalo.floatValue() >= tmp.floatValue()) {
                        ((Naleznosc) naleznosci.get(i)).zostalo = round(((Naleznosc) naleznosci.get(i)).zostalo.floatValue() - tmp.floatValue());
                        tmp = Float.valueOf(0.0F);
                    } else {
                        tmp = round(tmp.floatValue() - ((Naleznosc) naleznosci.get(i)).zostalo.floatValue());
                        ((Naleznosc) naleznosci.get(i)).zostalo = Float.valueOf(0.0F);
                    }
                } else {
                    tmp = Float.valueOf(0.0F);
                }
            }
        }
        wplaty.add(new Wplata(round(kwota.floatValue()), d));
        sumujNaleznosci();
        sumujWplaty();
    }

    public void dodajNal(Float kwota, Integer rok) {
        Naleznosc n = new Naleznosc(round(kwota.floatValue()), rok);
        if (naleznosc.floatValue() < sumaWplat.floatValue()) {
            Float roznica = round(sumaWplat.floatValue() - naleznosc.floatValue());
            if (roznica.floatValue() < kwota.floatValue()) {
                n.zostalo = round(n.zostalo.floatValue() - roznica.floatValue());
            } else {
                n.zostalo = Float.valueOf(0.0F);
            }
        }
        naleznosci.add(n);
        sumujNaleznosci();
    }

    private void sumujWplaty() {
        Float suma = Float.valueOf(0.0F);
        for (Iterator i$ = wplaty.iterator(); i$.hasNext();) {
            Wplata w = (Wplata) i$.next();
            suma = round(suma.floatValue() + w.kwota.floatValue());
        }

        sumaWplat = suma;
    }

    public Float getSumaZostalo() {
        Float ret = 0.0f;
        for (Naleznosc nn : (List<Naleznosc>) naleznosci) {
            ret += nn.zostalo;
        }
        return round(ret);
    //return round(naleznosc.floatValue() - sumaWplat.floatValue());
    }

    public void usunNal(int nr) {
        if (naleznosci.size() > nr) {
            naleznosci.remove(nr);
        }
        sumujNaleznosci();
    }

    public void usunWplate(int nr) {
        if (wplaty.size() > nr) {
            wplaty.remove(nr);
        }
        sumujWplaty();
        sumujNaleznosci();
    }

    public void setZapl(Boolean b) {
        wysl_zapl = b;
    }

    public void setUpom(Boolean b) {
        wysl_upom = b;
    }

    public void setTyt(Boolean b) {
        wysl_tyt = b;
    }

    public void setNip(String nnip) {
        nip = nnip;
    }

    public String getNip() {
        return nip;
    }

    @Override
    public String toString() {
        return (new StringBuilder()).append("").append(naleznosci).append(imie).append(nazwisko).append(naleznosc).append(wplaty).toString();
    }

    public String getIm() {
        return imie;
    }

    public void setIm(String txt) {
        imie = txt;
    }

    public String getNaz() {
        return nazwisko;
    }

    public void setNaz(String txt) {
        nazwisko = txt;
    }

    public String getPesel() {
        return pesel;
    }

    public void setPesel(String txt) {
        pesel = txt;
    }

    public Float getNal() {
        return naleznosc;
    }

    public Float getPow() {
        return pow;
    }

    public void setPow(Float p) {
        pow = p;
    }

    public List getLWpl() {
        return wplaty;
    }

    public List getLNal() {
        return naleznosci;
    }

    public boolean getZapl() {
        return wysl_zapl.booleanValue();
    }

    public void setZaplDate(Date d) {
        zaplDate = d;
    }

    public void setUpomDate(Date d) {
        upomDate = d;
    }

    public void setTytDate(Date d) {
        tytDate = d;
    }

    public Float getSumaNal() {
        return naleznosc;
    }

    public String getZaplDate() {
        if (wysl_zapl.booleanValue()) {
            SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy");
            String s = df.format(zaplDate);
            return s;
        } else {
            return "";
        }
    }

    public String getUpomDate() {
        if (wysl_upom.booleanValue()) {
            SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy");
            String s = df.format(upomDate);
            return s;
        } else {
            return "";
        }
    }

    public String getTytDate() {
        if (wysl_tyt.booleanValue()) {
            SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy");
            String s = df.format(tytDate);
            return s;
        } else {
            return "";
        }
    }

    public boolean getUpom() {
        return wysl_upom.booleanValue();
    }

    public boolean getTyt() {
        return wysl_tyt.booleanValue();
    }

    public String getNr() {
        return (new StringBuilder()).append("").append(nrSp).append(".").append(nrWsi).append(".").append(nrKli).toString();
    }

    public Float getBiezacaZaleglosc() {
        if (naleznosci.size() > 0) {
            int rokMax = 0;
            Naleznosc ret = ((Naleznosc) naleznosci.get(naleznosci.size() - 1));
            for (Naleznosc nn : (List<Naleznosc>) naleznosci) {
                if (nn.rok > rokMax) {
                    ret = nn;
                    rokMax = nn.rok;
                }
            }
            return ret.kwota;
        } else {
            return 0f;
        }
    }

    public void doKomornika(int nr) {
        if (komornik == null) {
            komornik = new ArrayList();
        }
        Naleznosc n = (Naleznosc) naleznosci.get(nr);
        komornik.add(new Wplata(n.zostalo, new Date()));
        n.zostalo = 0.0f;
        sumujNaleznosci();
        sumujWplaty();
    }

    public List getKomornik() {
        if (komornik == null) {
            komornik = new ArrayList();
        }
        return komornik;
    }

    public List<Naleznosc> getNiepusteZal() {
        List<Naleznosc> ret = new ArrayList<Naleznosc>();
        for (Naleznosc n : (List<Naleznosc>) naleznosci) {
            if (n.zostalo > 0f) {
                ret.add(n);
            }
        }
        return ret;
    }

    public Float getSumaKomornik() {
        Float ret = 0.0f;
        if (komornik != null) {
            for (Wplata n : (List<Wplata>) komornik) {
                ret += n.kwota;
            }
        }
        return round(ret);
    }

    public void usunKomornik(int nr) {
        if (komornik.size() > nr) {
            komornik.remove(nr);
        }
    }

    public Float getSumaWplat(Date from, Date to) {
        Float ret = 0f;
        for (Wplata w : (List<Wplata>) wplaty) {
            if (w.data.after(from) && w.data.before(to)) {
                ret += w.kwota;
            }
        }

        return ret;
    }
    public Integer nrSp;
    public Integer nrWsi;
    public Integer nrKli;
    public String ulica;
    public String miejscowosc;
    public String kod;
    private String imie;
    private String nazwisko;
    private String pesel;
    private String nip;
    private Float pow;
    private Float naleznosc;
    private Float sumaZostalo;
    private Float sumaWplat;
    private List naleznosci;
    private List wplaty;
    private Boolean wysl_zapl;
    private Date zaplDate;
    private Boolean wysl_upom;
    private Date upomDate;
    private Boolean wysl_tyt;
    private Date tytDate;
    private List komornik;
}
